package com.lhz.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.lhz.pojos.Staff;
import org.springframework.web.bind.annotation.RequestParam;
import com.lhz.services.StaffService;
/**
 *                 _ooOoo_
 *                o8888888o
 *                88" . "88
 *                (| -_- |)
 *                O\  =  /O
 *             ____/`---'\____
 *           .'  \\|     |//  `.
 *          /  \\|||  :  |||//  \
 *         /  _||||| -:- |||||-  \
 *         |   | \\\  -  /// |   |
 *         | \_|  ''\---/''  |   |
 *         \  .-\__  `-`  ___/-. /
 *       ___`. .'  /--.--\  `. . __
 *    ."" '<  `.___\_<|>_/___.'  >'"".
 *   | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *   \  \ `-.   \_ __\ /__ _/   .-` /  /
 *====`-.____`-.___\_____/___.-`____.-'======
 *                 `=---='
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *             佛祖保佑       不再单身
 *佛曰：
 *写字楼里写字间，写字间里程序员；
 *程序人员写程序，写完程序换酒钱；
*/
@Controller
@RequestMapping("/staff")
public class StaffControll {
	@Autowired
	private StaffService staffService;

	/**
	 * 跳到员工资料修改页面
	 * @return
	 */
	@RequestMapping("/toStaffAlter")
	public ModelAndView toStaffAlter(ModelAndView mv) {
		mv.setViewName("staffAlter");
		//通过shiro获取员工信息
		
		return mv;
	}
	
	/**
	 * 修改用户信息
	 * @param staff
	 * @return
	 */
	@RequestMapping("/updateStaffInfo")
	@ResponseBody
	public Map<String,String> updateStaffInfo(Staff staff) {
		Map<String,String> map=new HashMap<>();
		//通过shiro获取员工id
		staff.setStaffId("sta001");
		String result=staffService.updateStaffInfo(staff);
		if(result!="修改成功") {
			map.put("result", "修改失败");
			map.put("msg", result);
		}else {
			map.put("result", result);
		}
		return map;
	}
	
	/**
	 * 显示员工信息
	 * @param mv
	 * @return
	 */
	@RequestMapping(value="/staffshows")
	public ModelAndView staffshows(
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "10") int pageSize) {
		ModelAndView mv=new ModelAndView();
		Map<String, Object> svlist = staffService.staffshows(pageNum,pageSize);
		System.out.println(svlist);
		mv.addObject("svList",svlist);
		mv.setViewName("staffShow");
		return mv;
		
	}
	/**
	 * 条件查询员工信息
	 * @param staffId
	 * @param name
	 * @param sex
	 * @return
	 */
	@RequestMapping(value="/searchByshow")
	public ModelAndView searchByshow(String staffId,String name,String sex) {
		ModelAndView mv=new ModelAndView();
		Map<String, Object> svlist = staffService.searchByshow(
				staffId,name, sex);
		mv.addObject("svList",svlist);
		mv.setViewName("staffShow");
		return mv;
		
	}
	
	 /**
	  * 跳转添加员工界面
	  * @return
	  */
	@RequestMapping(value="/staffadds")
	public ModelAndView staffAdds() {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("staffAdd");
		return mv;
	}
	
	/**
	 * 通过staffId跳转显示界面
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value="/staffAlter")
	public ModelAndView staffAlter(String staffId) {
		ModelAndView mv=new ModelAndView();	
		Map<String, Object> info=staffService.staffAlter(staffId);
  		mv.addObject("svList", info);
 		mv.setViewName("staffAlter");
		return mv;
	}
	
	
	/**
	 * 添加员工
	 * @param sta
	 * @return
	 */
	@RequestMapping(value="/savestaff")
	public ModelAndView savestaff( Staff sta) {
		ModelAndView mv=new ModelAndView();
		Map<String, Object> svlist = staffService.stfaddall(sta);
		mv.addObject("svList",svlist);
		mv.setViewName("staffShow");
		return mv;
	}
	
	/**
	 * 状态查询
	 * @param status
	 * @return
	 */
	@RequestMapping(value="/searchByStaff")
	public ModelAndView searchByStaff(Long status) {
		ModelAndView mv=new ModelAndView();
		Map<String, Object> status2 = staffService.searchByStatus(status);
		mv.addObject("svList",status2);
		mv.setViewName("staffShow");
		return mv;
	}
	
	/**
	 * 图片上传
	 * @param staffId
	 * @param staffimg
	 * @param file
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping(value="/uploadStaffImg")
	@ResponseBody
	public Map<String, Object> uploadStaffImg(String staffId,MultipartFile file){
		try {
			return  staffService.uploadStaffImg(staffId, file);
		} catch (IllegalStateException | IOException e) {
  			e.printStackTrace();
		}
		throw new RuntimeException("图片上传有错误...");
	}
	
	/**
	 * 员工删除
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value="/delectStaffStatus",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delectStaffStatus(String staffId) {
		return staffService.delectStaffStatus(staffId);
	}
	/**
	 * 设置员工的状态
	 * @param staffId
	 * @param status
	 * @return
	 */
	@RequestMapping(value="/updateState",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateState(String staffId,String status){
		return staffService.updateState(staffId, status);
	}
	
	/**
	 * 根据id来修改员工信息
	 * @param staffId
	 * @param sta
	 * @return
	 */
	@RequestMapping(value="/updateEmployee",method=RequestMethod.POST)
	@ResponseBody
	public  Map<String,Object> updateEmployee(Staff sta){
		return 	staffService.updateEmployee(sta);
	}
	
	@RequestMapping(value="/table")
	public ModelAndView table() {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("table_element");
		return mv;
	}
	
	
}
